package com.team.game.Game.Wizards.FireWizard;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Game.Impacts;
import com.team.game.Game.Levels;
import com.team.game.framework.Abstract.GesturesInfo;
import com.team.game.framework.Abstract.Spell;
import com.team.game.framework.Game;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;
import com.team.game.framework.impl.SurfaceThread;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;
import static com.team.game.framework.impl.Scale_X_Y.relation;

public class Meteoryte extends Spell {
    private ArrayList<Bitmap> Meteoryte;
    private PointF[] pointF = new PointF[3];
    private Rect[] RectSource = new Rect[3];
    private Bitmap MeteorBoom;
    private boolean Boom[] = new boolean[3];
    private RectF StartPos;
    private boolean[] Tap = new boolean[3];
    private int TapCounter = 0;
    private float Damage;
    private float Radius;

    private int BoomCounter[] = new int[3];
    private RectF[] CurrentRect = new RectF[3];


    public Meteoryte () {
        super ();

        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/hero_1/spell_4.png");
            icon_spell = BitmapFactory.decodeStream (inputStream);
        } catch (IOException e) {
            e.printStackTrace ();
        }
        cost = new int[]{15, 25, 60, 90};
    }

    public Meteoryte(Game game) {
        super (game);

        TimeRecharch = 4.5f;
        ProgressBarRect.set(19 * Scale_X_Y.scale, 171 * Scale_X_Y.scale, 69 * Scale_X_Y.scale, 221 * Scale_X_Y.scale);
        RectDstForBlackProgress.set(21 * Scale_X_Y.scale, 173 * Scale_X_Y.scale, 67 * Scale_X_Y.scale, 219 * Scale_X_Y.scale);
        TopOfRectProgressDst = RectDstForBlackProgress.top;


        Damage = 30;
        Radius = 200 * Scale_X_Y.scale;
        StartPos = new RectF(0, 0, 0, 0);
        for (int i = 0; i < 3; i++) {
            CurrentRect[i] = new RectF();
            RectSource[i] = new Rect();
            pointF[i] = new PointF();

        }
        this.restart();

    }

    @Override
    public boolean updateSpell() {
        for (int i = 0; i < 3; i++) {
            if (Tap[i]) {
                if (Boom[i]) {
                    if (BoomCounter[i] == 19) {
                        continue;
                    } else if (BoomCounter[i] == 0) {
                        CurrentRect[i].left = pointF[i].x - MeteorBoom.getHeight() * Scale_X_Y.scale / 2;
                        CurrentRect[i].right = pointF[i].x + MeteorBoom.getHeight() * Scale_X_Y.scale / 2;
                        CurrentRect[i].top = pointF[i].y - MeteorBoom.getHeight() * Scale_X_Y.scale / 2;
                        CurrentRect[i].bottom = pointF[i].y + MeteorBoom.getHeight() * Scale_X_Y.scale / 2;


                    } else if (BoomCounter[i] == 7)
                        //game.getCurrentScreen ().getLevel ().impactOnTheRadius (pointF[i], Damage, Radius);
                        Impacts.impactOnTheRadius (level, pointF[i], Damage, Radius);
//                if (time > 0.02) {
//                    time = 0;
                    RectSource[i].set(216 * BoomCounter[i], 0, 216 + 216 * BoomCounter[i], 216);
                    BoomCounter[i]++;
//                }
//                else
//                    time += SurfaceThread.deltaTime;
                } else {
                    CurrentRect[i].top += 10 * 187.5 * SurfaceThread.deltaTime;
                    CurrentRect[i].bottom += 10 * 187.5 * SurfaceThread.deltaTime;
                    if (CurrentRect[i].top > pointF[i].y - CurrentRect[i].height()) {
                        Boom[i] = true;
                    }
                }
            }
        }
        if (!WaitForTap)
            time += SurfaceThread.deltaTime;
        if (BoomCounter[0] == 19 && BoomCounter[1] == 19 && BoomCounter[2] == 19 && time > TimeRecharch) {
            this.restart();
            return false;
        }
        return true;
    }

    @Override
    public boolean activeSpell(GesturesInfo gesturesInfo1) {
        level = (Levels) game.getCurrentScreen ().getLevel ();
        Active = true;
        return true;
    }

    @Override
    public boolean setMotionEventSpell(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            Active = true;
            pointF[TapCounter].set(motionEvent.getX(), motionEvent.getY());
            CurrentRect[TapCounter].set((motionEvent.getX() - Meteoryte.get(0).getWidth() / 2 * Scale_X_Y.Scale_X),
                    -Meteoryte.get(0).getHeight() * Scale_X_Y.Scale_Y,
                    (motionEvent.getX() + Meteoryte.get(0).getWidth() / 2 * Scale_X_Y.Scale_X),
                    0);
            Tap[TapCounter] = true;
            TapCounter++;
            if (TapCounter > 2) {
                WaitForTap = false;
                return false;
            }
        }
        return true;
    }

    @Override
    public void drawSpell(Canvas canvas) {
        for (int i = 0; i < 3; i++) {
            if (Tap[i]) {
                if (Boom[i]) {
                    canvas.drawBitmap(MeteorBoom, RectSource[i], CurrentRect[i], null);
                } else
                    canvas.drawBitmap(Meteoryte.get(0), null, CurrentRect[i], null);
            }
        }
    }

    @Override
    public void restart() {
        this.progressBarRestart();
        WaitForTap = true;
        for (int i = 0; i < 3; i++) {
            Tap[i] = false;
            CurrentRect[i].set(StartPos);
            TapCounter = 0;
            RectSource[i].set(0, 0, 0, 0);
            pointF[i].set(0, 0);
            Boom[i] = false;
            BoomCounter[i] = 0;
        }
    }


    @Override
    public void dispose() {

    }

    @Override
    public void setImages () {
        Meteoryte = new ArrayList<> (6);

        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Meteoryte/meteor.png");
            Meteoryte.add (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Meteoryte/FireBallBoom.png");
            MeteorBoom = (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Meteoryte/Meteor_Normal.png");
            ProgressBarNormal = (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Meteoryte/Meteor_Active.png");
            ProgressBarActive = (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Meteoryte/Meteor_RActive.png");
            ProgressBarRActive = (BitmapFactory.decodeStream (inputStream));

        } catch (IOException e) {
            e.printStackTrace ();
        }

        SPELL_NAME = "fire_meteor";
    }

    @Override
    public void drowProfileExample (Canvas canvas, int A) {
        paintText.setTextSize (30 * relation);
        paintText.setColor (Color.RED);
        canvas.drawText ("Working on it\n\n\n\nDamage:?/?/?/?", 660 * Scale_X, 900 * Scale_Y, paintText);
    }

    @Override
    public void updateProfileExample() {

    }

    @Override
    public void setStartConfiguration() {

    }
}
